/**
 * $RCSfile: DefaultUserProvider.java,v $
 * $Revision: 3116 $
 * $Date: 2005-11-24 06:25:00 -0300 (Thu, 24 Nov 2005) $
 *
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.lzy.pt;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.xmpp.packet.JID;

public class DefaultBaseInfoProvider implements BaseInfoProvider{

    private static final String LOAD_BASEINFO =
            "SELECT * FROM oftBaseInfo WHERE username=? order by T_DateTime desc";
    
    private static final String LOAD_BASEINFO1 =
            "SELECT * FROM oftBaseInfo1 WHERE username=?";

    public Map<String, Object> loadBaseInfoMap(String username) throws Exception{
    	if(username.contains("@")) {
            if (!XMPPServer.getInstance().isLocal(new JID(username))) {
//                throw new UserNotFoundException("Cannot load user of remote server: " + username);
            }
            username = username.substring(0,username.lastIndexOf("@"));
        }
        Connection conn = DbConnectionManager.getConnection();
        
    	QueryRunner runner = new QueryRunner();
    	Map<String, Object> list = runner.query(conn,LOAD_BASEINFO, new MapHandler(),username);
    	DbConnectionManager.closeConnection(conn);
    	return list;
    }
    
    
    public List<BaseInfo> loadBaseInfo(String username) throws Exception{
    	if(username.contains("@")) {
            if (!XMPPServer.getInstance().isLocal(new JID(username))) {
//                throw new UserNotFoundException("Cannot load user of remote server: " + username);
            }
            username = username.substring(0,username.lastIndexOf("@"));
        }
        Connection conn = DbConnectionManager.getConnection();
        
    	ResultSetHandler<List<BaseInfo>> beanHandler = new BeanListHandler<BaseInfo>(BaseInfo.class);
    	QueryRunner runner = new QueryRunner();
    	List<BaseInfo> list = runner.query(conn,LOAD_BASEINFO, beanHandler,username);
    	DbConnectionManager.closeConnection(conn);
    	return list;
    }
    
    @Override
    public List<BaseInfo1> loadBaseInfo1(String username) throws Exception {
    	if(username.contains("@")) {
            if (!XMPPServer.getInstance().isLocal(new JID(username))) {
//                throw new UserNotFoundException("Cannot load user of remote server: " + username);
            }
            username = username.substring(0,username.lastIndexOf("@"));
        }
    	
    	Connection conn = DbConnectionManager.getConnection();
        
    	ResultSetHandler<List<BaseInfo1>> beanHandler = new BeanListHandler<BaseInfo1>(BaseInfo1.class);
    	QueryRunner runner = new QueryRunner();
    	List<BaseInfo1> list = runner.query(conn,LOAD_BASEINFO1, beanHandler,username);
    	DbConnectionManager.closeConnection(conn);
    	return list;
    }
}
